import 'package:bwt_ad_flutter/BwtAdCore.dart';
import 'package:bwt_ad_flutter/ad/loader/BwtAdLoader.dart';
import 'package:bwt_ad_flutter/ad/widget/nativeWidget/BwtNativeAd.dart';
import 'package:bwt_ad_flutter/ad/widget/nativeWidget/BwtNativeAdWidget.dart';

import '../widget/nativeWidget/BwtNativeAdData.dart';

class BwtNativeAdLoader extends BwtAdLoader<BwtNativeAd> {
  OnBwtNativeAdEvent<BwtNativeAd>? onNativeAdLoad;
  /// 加载广告
  loadAd({
    required String posId,
    required double adWidth,
    required double adHeight,
  }) {
    load(
      posId: posId,
      methodName: BwtAdCore.methodLoadNativeAd,
      arguments: {
        BwtAdCore.keyAdWidth: adWidth,
        BwtAdCore.keyAdHeight: adHeight,
      },
    );
  }

  closeAd(String adId){
    BwtAdCore.invokeMethod(method: "nativeAdCloseAd",params:{"adId":adId});
  }

  @override
  onAdLoadCallback(BwtNativeAd bwtAd) {
    bwtAd.nativeAdview = BwtNativeAdWidget(
      bwtNativeAd: bwtAd,
      viewType: BwtAdCore.viewTypeBwtNativeAd,
    );

    return super.onAdLoadCallback(bwtAd);
  }

  onNativeAdLoadCallback(BwtNativeAd bwtAd,BwtNativeAdData nativeAdData){
    bwtAd.nativeAdview = BwtNativeAdWidget(
      bwtNativeAd: bwtAd,
      viewType: BwtAdCore.viewTypeBwtNativeAd,
    );
    onNativeAdLoad?.call(bwtAd,nativeAdData);
  }

  @override
  BwtNativeAd createBwtAd(
      {required String adId, required Map<String, dynamic>? arguments}) {
    return BwtNativeAd(
        adId: adId,
        adLoaderId: adLoaderId,
        adWidth: arguments?[BwtAdCore.keyAdWidth] ?? double.infinity,
        adHeight: arguments?[BwtAdCore.keyAdHeight] ?? double.infinity);
  }
}
typedef OnBwtNativeAdEvent<BwtNativeAd> = void Function(BwtNativeAd bwtAd,BwtNativeAdData nativeAdData);